﻿namespace Admin.NET.Application.Service;

/// <summary>
/// 公用ViewModel子表,用在主从业务中
/// </summary>
public class BasViewModelDetailService : BaseService<BasViewModelDetail>
{

    public BasViewModelDetailService(IServiceScopeFactory scopeFactory) : base(scopeFactory)
    {
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<BasViewModelDetail>> Page(BasViewModelDetailPageInput input)
    {
        return await Queryable
           .WhereIF(input.BasViewModeId > 0, it => it.BasViewModeId == input.BasViewModeId)
           .WhereIF(input.Keyword.HasValue(), it => SqlFunc.JsonLike(it.Model, input.Keyword))
           .ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 通过Id获取
    /// </summary>
    public async Task<BasViewModelDetail> Get(long id)
    {
        var obj = await _rep.InSingleAsync(id);
        return obj;
    }

    /// <summary>
    /// 新增成功后返回主键
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<long> Add(BasViewModelDetailAddInput input)
    {
        var entity = new BasViewModelDetail() { BasViewModeId = input.BasViewModeId, Model = input.Model };
        var obj = await _rep.InsertReturnEntityAsync(entity);
        return obj.Id;
    }

    /// <summary>
    /// 更新成功后返回true
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<bool> Update(BasViewModelDetailUpdateInput input)
    {
        var entity = new BasViewModelDetail() { Id = input.Id, BasViewModeId = input.BasViewModeId, Model = input.Model };
        return await _rep.UpdateAsync(entity);
    }
}